import java.util.Arrays;

public class Solution {
    public int carFleet(int target, int[] position, int[] speed) {
        int len=position.length;
        Car[] car=new Car[len];
        for(int i=0;i<len;i++){
            car[i]=new Car(position[i],(double)(target-position[i])/speed[i]);
        }
        Arrays.sort(car,(a,b)->Integer.compare(a.position,b.position));
        int ans=0;
        int t=len;
        while(--t>0){
            if(car[t].time<car[t-1].time){
                ans++;
            }else{
                car[t-1]=car[t];
            }
        }
        return ans+(t==0?1:0);
    }
    class Car{
        int position;
        double time;
        public Car(int position,double time){
            this.position=position;
            this.time=time;
        }
    }




public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {

    }
    TreeNode(int val) {
        this.val = val;
    }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
    int index=0;
    int count=0;
    public int kthSmallest(TreeNode root, int k) {
        count=k;
        dfs(root);
        return index;
    }
    public void dfs(TreeNode root){
        if(root==null){
            return ;
        }
        dfs(root.left);
        count--;
        if(count==0){
            index=root.val;
            return;
        }
        dfs(root.right);
    }




    public void sortColors(int[] nums) {
        int gap=nums.length;
        while(gap>1){
            Mysort(gap,nums);
            gap=(gap/3)+1;
        }
        Mysort(1,nums);
    }
    public void Mysort(int gap,int[] nums){
        for(int i=1;i<nums.length;i++){
            int j=i-gap;
            int tmp=nums[i];
            for(;j>=0&&nums[j]>tmp;j-=gap){
                nums[j+gap]=nums[j];
            }
            nums[j+gap]=tmp;
        }
    }



    public void sortColors3(int[] nums) {
        if(nums.length==0||nums==null){
            return ;
        }
        for(int i=1;i<nums.length;i++){
            int j=i-1;
            int tmp=nums[i];
            for(;j>=0&&nums[j]>tmp;j--){
                nums[j+1]=tmp;
            }
            nums[j]=tmp;
        }
    }





    public void sortColors2(int[] nums){
        if(nums==null||nums.length==0){
            return;
        }
        for(int i=1;i<nums.length;i++){
            int tmp=nums[i];
            for(int j=i-1;j>=0;j--){
                if(nums[j]>tmp){
                    nums[j+1]=nums[j];
                    nums[j]=tmp;
                }
            }
        }
    }



    public void sortColors1(int[] nums) {
        if(nums==null||nums.length==0){
            return;
        }
        Arrays.sort(nums);
    }
}
class Solution2 {
    public int carFleet(int target, int[] position, int[] speed) {
        int len=position.length;
        Car[] car=new Car[len];
        for(int i=0;i<len;i++){
            car[i]=new Car(position[i],(double)(target-position[i])/speed[i]);
        }
        Arrays.sort(car,(a,b)->Integer.compare(a.position,b.position));
        int ans=0;
        int t=len;
        while(--t>0){
            if(car[t].time<car[t-1].time){
                ans++;
            }else{
                car[t-1]=car[t];
            }
        }
        return ans+(t==0?1:0);
    }
    class Car{
        int position;
        double time;
        public Car(int position,double time){
            this.position=position;
            this.time=time;
        }
    }




    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {

        }
        TreeNode(int val) {
            this.val = val;
        }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    int index=0;
    int count=0;
    public int kthSmallest(TreeNode root, int k) {
        count=k;
        dfs(root);
        return index;
    }
    public void dfs(TreeNode root){
        if(root==null){
            return ;
        }
        dfs(root.left);
        count--;
        if(count==0){
            index=root.val;
            return;
        }
        dfs(root.right);
    }




    public void sortColors(int[] nums) {
        int gap=nums.length;
        while(gap>1){
            Mysort(gap,nums);
            gap=(gap/3)+1;
        }
        Mysort(1,nums);
    }
    public void Mysort(int gap,int[] nums){
        for(int i=1;i<nums.length;i++){
            int j=i-gap;
            int tmp=nums[i];
            for(;j>=0&&nums[j]>tmp;j-=gap){
                nums[j+gap]=nums[j];
            }
            nums[j+gap]=tmp;
        }
    }



    public void sortColors3(int[] nums) {
        if(nums.length==0||nums==null){
            return ;
        }
        for(int i=1;i<nums.length;i++){
            int j=i-1;
            int tmp=nums[i];
            for(;j>=0&&nums[j]>tmp;j--){
                nums[j+1]=tmp;
            }
            nums[j]=tmp;
        }
    }





    public void sortColors2(int[] nums){
        if(nums==null||nums.length==0){
            return;
        }
        for(int i=1;i<nums.length;i++){
            int tmp=nums[i];
            for(int j=i-1;j>=0;j--){
                if(nums[j]>tmp){
                    nums[j+1]=nums[j];
                    nums[j]=tmp;
                }
            }
        }
    }



    public void sortColors1(int[] nums) {
        if(nums==null||nums.length==0){
            return;
        }
        Arrays.sort(nums);
    }
}